<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\TmpService;
use Illuminate\Http\Request;

class TmpController extends BaseController
{
    public function __construct(TmpService $service)
    {
        $this->db = $service;
    }

    /**
     * 列表获取
     */
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 5);
        $rst = $this->db->getList($pages);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data'], $rst['total']);
    }

    /**
     * 列表获取
     */
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $rst = $this->db->getOne($id);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }

    /**
     * 添加
     */
    public function add(Request $request)
    {
        $title = $request->post('title', '');
        $rst = $this->db->add($title);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson();
    }

    /**
     * 修改
     */
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $title = $request->post('title', '');
        $rst = $this->db->modify($id, $title);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }

    /**
     * 上传图片
     */
    public function uploadImg(Request $request)
    {
        $id = $request->post('id', 0);
        $img = $request->post('img', '');
        $rst = $this->db->uploadImg($id, $img);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson();
    }

    /**
     * 属性修改
     */
    public function attrModify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['tmp_id'] = $request->post('tmp_id', 0);
        $pas['attr_name'] = $request->post('attrKey', '');
        $pas['val'] = $request->post('attrVal', '');
        if (!$pas['attr_name']) {
            return $this->toJson(-2, '属性名必须的');
        }
        if (!$id) {
            $res = $this->db->attrAdd($pas);
        } else {
            $res = $this->db->attrModify($id, $pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 关注列表
     */
    public function getFollowList(Request $request)
    {
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $tmpId = $request->get('tmpId', 0);
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 5);
        $rst = $this->db->getFollowList($tmpId, $pages);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }

    /**
     * 评论列表获取
     */
    public function getCommentList(Request $request)
    {
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $tmpId = $request->get('tmpId', 0);
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 5);
        $rst = $this->db->getCommentList($tmpId, $pages);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }
}